package com.lemon.main;

import com.lemon.business.InsertDataService;
import com.lemon.business.hook.ShutDownHookService;
import com.lemon.business.timedtask.TimedTaskService;
import com.lemon.config.Config;
import com.lemon.service.ServerNetty;
import com.lemon.utils.DBUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 程序入口
 * @author lenomSeven 1337857379@qq.com
 * @date 2020-10-6 16:37
 */
public class MainClass {
    private static final Logger logger = LoggerFactory.getLogger(MainClass.class);

    public static void main(String[] args) {
        try {
            initConfig(); // 初始化配置
            startInsertThread(); // 启入库线程
            startTimedTask();// 启定时任务
            Runtime.getRuntime().addShutdownHook(new Thread(new ShutDownHookService())); // 添加关闭钩子
            startService(); // 启netty服务
        } catch (Exception e) {
            logger.error("server start fail.", e);
        }
    }

    private static void startTimedTask(){
        TimedTaskService.start();
    }

    private static void startInsertThread(){
        Thread thread = new Thread(new InsertDataService());
        thread.setDaemon(false);
        thread.start();
    }

    /**
     * 初始化zk和本地的配置
     */
    private static void initConfig(){
        Config.init();
        DBUtil.initC3P0ConnectionPool();
    }

    private static void startService() throws InterruptedException {
        ServerNetty serverNetty = new ServerNetty(Config.servicePort);
        serverNetty.start();
    }
}
